// pages/updateNumber/updateNumber.js
var app = getApp();
const util = require("../../utils/util")
const constant = require("../../common/constant")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    accountType:"c",
    counting:false,
    timeCountDown:"获取验证码",
    mobile:"",
    code:""
  },
  bindNumInput:function(e){
    this.setData({
      number:e.detail.value
    })
  },
  bindCodeInput:function(e){
    this.setData({
      code:e.detail.value
    })
  },
  bindSubmit:function(){
    let that = this;
    let url = "https://groomtea.icebartech.com/api/miniApp/dealer/updateMobile";
    if(this.data.accountType=="b"){
      url = "https://groomtea.icebartech.com/api/miniApp/salesman/updateMobile"
    }
    wx.showLoading({
      title: '加载中',
    })
    util.post({
      url:url,
      data: { "code": that.data.code, "mobile": that.data.mobile },
      success:(res)=>{
        // getApp().globalData.userInfo.tempCode = that.data.code
        // getApp().globalData.userInfo.mobile = that.data.number
        console.log(res)
        if (res.status == '200') {
          wx.navigateTo({
            url:"../updateNewNumber/index"
          }) 
        };
        wx.hideLoading();
      },
      fail:(res)=>{
      
       
      },
      
      complete:function(){
          wx.hideLoading()
      }
    })
  },
  bindGetCode:function(){
    if (this.data.timeCountDown !== '获取验证码')return;
    let that = this
    wx.showLoading({
      title: '加载中',
    })
    util.post({
      url:"https://groomtea.icebartech.com/api/miniApp/salesman/findMobileCode",
      data: { "bizType": "change_mobile", "mobile": that.data.mobile },
      success:(res)=>{
        console.log(res) 
        that.countDown(that,60)
        wx.hideLoading()
        app.titleToast('验证码已发送')
        this.setData({
          code:res.data.bussData.code
        })
      },
      
      fail:(error)=>{
        console.log(error)
        wx.hideLoading()
        app.titleToast(error.msg)
      },
      complete:function(){
          wx.hideLoading()
      }
    })
  },
   countDown:function(that,count) {
    if (count == 0) {
      that.setData({
        timeCountDown: '获取验证码',
        counting:false
      })
      return;
    }
  
    that.setData({
      counting:true,
      timeCountDown: count + '秒后重新获取',
    })
    
    setTimeout(function(){
      count--;
      that.countDown(that, count);
    }, 1000);
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    const user = app.globalData.userInfo;
    console.log(user);
    if (!user.dealerType){
       this.setData({
        accountType:'b',
       })
    };
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      mobile:app.globalData.userInfo.mobile,
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})